public without sharing class ShiyeApplicationGoodsDetailsHandler {

    public void  test(){

    }
    // 新增或者更新时 汇总字段
    /*
    public static void SummaryGoodsDetail(list<ShiyeApplicationGoodsDetails__c> list_new) {
        
        Set<Id> pIds = new Set<Id>();
        for (ShiyeApplicationGoodsDetails__c sa : list_new) {
            if (sa.ShiyeApplicationGoods__c != null) {
                pIds.add(sa.ShiyeApplicationGoods__c);
            }
        }
        
        // 低于公司价统计
        Map<Id, AggregateResult> cntPrice = new Map<Id, AggregateResult>([
                select count(Id) Cnt_Id, ShiyeApplicationGoods__c Id
                  from ShiyeApplicationGoodsDetails__c
                 where ShiyeApplicationGoods__c in : pIds
                 and PriceBelowCompanyPrice__c = '是'
                 group by ShiyeApplicationGoods__c
        ]);
        // 分子血型统计
        Map<Id, AggregateResult> cntDS = new Map<Id, AggregateResult>([
                select count(Id) Cnt_Id, ShiyeApplicationGoods__c Id
                  from ShiyeApplicationGoodsDetails__c
                 where ShiyeApplicationGoods__c in : pIds
                 and ProductLine__c in ('分子', '血型','第三方产品')
                 group by ShiyeApplicationGoods__c
        ]);
        // 生化免疫产品统计
        Map<Id, AggregateResult> cntAE = new Map<Id, AggregateResult>([
                select count(Id) Cnt_Id, ShiyeApplicationGoods__c Id
                  from ShiyeApplicationGoodsDetails__c
                 where ShiyeApplicationGoods__c in : pIds
                 and ProductLine__c in ('生化', '免疫')
                 group by ShiyeApplicationGoods__c
        ]);
        // 血液体液微生物统计
        Map<Id, AggregateResult> cntBCT = new Map<Id, AggregateResult>([
                select count(Id) Cnt_Id, ShiyeApplicationGoods__c Id
                  from ShiyeApplicationGoodsDetails__c
                 where ShiyeApplicationGoods__c in : pIds
                 and ProductLine__c in ('血液', '体液', '微生物')
                 group by ShiyeApplicationGoods__c
        ]);
    // 标本采集产品统计
        Map<Id, AggregateResult> cntBBC = new Map<Id, AggregateResult>([
                select count(Id) Cnt_Id, ShiyeApplicationGoods__c Id
                  from ShiyeApplicationGoodsDetails__c
                 where ShiyeApplicationGoods__c in : pIds
                 and ProductLine__c in ('样本采集器')
                 group by ShiyeApplicationGoods__c
        ]);

        List<ShiyeApplicationGoods__c> saList = new List<ShiyeApplicationGoods__c>();
        for (Id pid : pIds) {
            ShiyeApplicationGoods__c sa = new ShiyeApplicationGoods__c(Id = pid);
            if (cntPrice.get(pid) != null) {
                sa.PriceBelowCompanyPrice__c = Integer.valueOf(cntPrice.get(pid).get('Cnt_Id'));
            }else{
            sa.PriceBelowCompanyPrice__c = 0;
            }
            if (cntDS.get(pid) != null) {
                sa.DStatistics__c = Integer.valueOf(cntDS.get(pid).get('Cnt_Id'));
            }else{
            sa.DStatistics__c = 0;
            }
            if (cntAE.get(pid) != null) {
                sa.AEStatistics__c = Integer.valueOf(cntAE.get(pid).get('Cnt_Id'));
            }else{
            sa.AEStatistics__c = 0;
            }
            if (cntBCT.get(pid) != null) {
                sa.BCtStatistics__c = Integer.valueOf(cntBCT.get(pid).get('Cnt_Id'));
            }else{
            sa.BCtStatistics__c = 0;
            }
            if (cntBBC.get(pid) != null) {
                sa.D2__c = Integer.valueOf(cntBBC.get(pid).get('Cnt_Id'));
            }else{
            sa.D2__c = 0;
            }
            saList.add(sa);
        }
        update saList;
    }
    ///删除时候重新统计
     public static void SummaryGoodsDetailDelete(list<ShiyeApplicationGoodsDetails__c> list_old) {
       
         
         Set<Id> pIds = new Set<Id>();
       for (ShiyeApplicationGoodsDetails__c sa : list_old) {
            if (sa.ShiyeApplicationGoods__c != null) {
                pIds.add(sa.ShiyeApplicationGoods__c);
            }
        }
        for(ShiyeApplicationGoods__c s : [select Status__c from ShiyeApplicationGoods__c where Id in :pIds]){
        
            if(s.Status__c == '审批中' || s.Status__c  == '已批准'){
                list_old[0].addError('实业借货申请审批状态为:' + s.Status__c +'. 不能删除该明细!');
                
                break;
            
            }
        
        } 

        
    } 
    
     public static void matchPrice(list<ShiyeApplicationGoodsDetails__c> list_new) {
     ///合同产品
         Set<Id>cpIds = new Set<Id>();
        for (ShiyeApplicationGoodsDetails__c gd : list_new){
            
        if(gd.Product__c  != null){
            cpIds.add(gd.Product__c);
            }
        }
        Set<Id>proIds = new Set<Id>();
        ///保存合同产品 及对应的产品和公司
        Map<Id,String>cp_com = new Map<Id ,String>();
         ///如果公司和产品为空的时候可能有问题 
        Map<String ,ContractProduct__c>cps = new Map<String,ContractProduct__c>();
        for (ContractProduct__c cp : [select Id,Contract__r.CompanyControls__c ,Product__c from ContractProduct__c where Id in : cpIds ]){
        cps.put(cp.Contract__r.CompanyControls__c + cp.Product__c,cp);
            proIds.add(cp.Product__c);
            cp_com.put(cp.Id,cp.Contract__r.CompanyControls__c + cp.Product__c);
        } 
       
        ///公司低价(元) 应调整价格(元)
       // Map<String ,Price__c> prs = new Map<String ,Price__c>();
        Map<Id ,Price__c> cp_prs = new Map<Id,Price__c>();
        for (Price__c p : [select Product__c,Company__c,CompanyBottomPrice__c,HospitalBottomPrice__c
                           from Price__c where Product__c in: proIds]){
           for(Id key : cp_com.keySet()){
           if(cp_com.get(key) == p.Company__c + p.Product__c ){
                     cp_prs.put(key,p);  
                }
           }
          // prs.put(p.Company__c + p.Product__c ,p);
           
        }
        
        Integer m = Date.today().month(); //当前月份
        Integer y = Date.today().year();  //当前年份 
       ///取上月
         if(m == 1){
        m = 12 ;
            y -= 1;
        }else{
        m -= 1;
            
        }
        
        String  month = String.valueOf(m) + '月';
        String  year= String.valueOf(y) + '年';
        //上月平均价(元)
         Map<Id ,AveragePrice__c>lastPs = new Map<Id ,AveragePrice__c>();
         for(AveragePrice__c a : [select AveragePrice__c  ,Product__c,Company__c from AveragePrice__c where 
                                  Year__c =:year and Month__c =: month and Product__c in : proIds]){
        
           for(Id key : cp_com.keySet()){
           if(cp_com.get(key) == a.Company__c + a.Product__c ){
                     lastPs.put(key,a);  
                }
           }
         }
        
         //挂网流水号
         Map<Id,NetSerialNumber__c> nets = new Map<Id ,NetSerialNumber__c>();
         for (NetSerialNumber__c n : [select NetSerialNumber__c ,Product__c,Company__c 
        
            for(Id key : cp_com.keySet()){
           if(cp_com.get(key) == n.Company__c + n.Product__c ){
                     nets.put(key,n);  
                }
           }
         }
         
        // System.debug('价格:' + cp_prs);
        for (ShiyeApplicationGoodsDetails__c gd : list_new){
        if(cp_prs.containsKey(gd.Product__c)){
                Price__c p = cp_prs.get(gd.Product__c);
            gd.CompanyPrice__c = p.CompanyBottomPrice__c;
                gd.PriceAdjustment__c = p.HospitalBottomPrice__c;
            }else{
            gd.CompanyPrice__c = null;
                gd.PriceAdjustment__c = null;
            }
            if(lastPs.containsKey(gd.Product__c)){
                AveragePrice__c a = lastPs.get(gd.Product__c);
            gd.AveragePriceLastMonth__c = a.AveragePrice__c;
            }else{
            gd.AveragePriceLastMonth__c = null;
            }
            if(nets.containsKey(gd.Product__c)){
                NetSerialNumber__c n = nets.get(gd.Product__c);
            gd.NetSerialNumber__c = n.NetSerialNumber__c;
            }else{
            gd.NetSerialNumber__c = null;
            }
           //System.Debug('暗号:'+gd);

        }         
         
     }
     */

}